﻿using DictionaryWebApp.DAL;
using DictionaryWebApp.DTO;
using DictionaryWebApp.EF_Models;
using DictionaryWebApp.Helpers;
using System;
using System.Diagnostics.Contracts;
using System.Web.Http;

namespace DictionaryWebApp.APIControllers
{
    [Authorize]
    public class DictionaryController : ApiController
    {
        private IDictionaryRepository _dictionaryRepository;

        public DictionaryController(IDictionaryRepository dictionaryRepository)
        {
            Contract.Requires<ArgumentNullException>(dictionaryRepository != null);

            _dictionaryRepository = dictionaryRepository;
        }

        [HttpGet]
        public IndexWordViewModel[] GetWords()
        {
            var id = User.Identity.GetUserId();

            return _dictionaryRepository.GetWords(id);
        }

        [HttpPost]
        public void AddWord([FromBody]NewWordViewModel word)
        {
            Contract.Requires<ArgumentNullException>(word != null);

            var id = User.Identity.GetUserId();

            _dictionaryRepository.AddWord(word, id);
        }

        [HttpDelete]
        public void DeleteWord([FromUri]string wordGuid)
        {
            Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(wordGuid));

            var userId = User.Identity.GetUserId();

            _dictionaryRepository.DeleteWord(wordGuid, userId);
        }
    }
}
